package slicktemplate;

import java.util.ArrayList;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author andrew
 */
public class TurretDynamitePlacer implements Updateable, Renderable {

    static public ArrayList<TurretDynamitePlacer> all = new ArrayList<TurretDynamitePlacer>();
    float x, y;
    Image sprite;
    Sound placesound;

    TurretDynamitePlacer(float x, float y) {
        if (all.isEmpty() && WallPlacer.all.isEmpty()) {
            all.add(this);
            this.x = x;
            this.y = y;
            try {
                sprite = new Image("sprites/Frame1TNT.png");
                placesound = new Sound("sounds/buildingPlaced.ogg");
            } catch (Exception e) {
                System.out.println("Failed to load the sprite \"sprites/wall.png\"");
            }
        }
    }

    @Override
    public boolean update(Input input, StateBasedGame sb) {
        x = (Math.round(input.getMouseX() / 32) * 32);
        y = (Math.round(input.getMouseY() / 32) * 32);
        if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            if (!Collideable.getCollisionLists(new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1), Level1.placeOverList)) {

                placesound.play();

                ((Level1) sb.getCurrentState()).addToGrid(new TurretDynamite(x, y), (int) x, (int) y);

                //if (!input.isKeyDown(Input.KEY_LSHIFT)) {
                    return true; //unless we're creating more, delete this
                //}
            }
        }
        return false;
    }

    @Override
    public void render(Graphics g) {
        sprite.draw(x, y);
    }

    public boolean mousePressed(Input input, StateBasedGame sb, int button) {
        return false;

    }

    public boolean mouseDown(Input input, StateBasedGame sb, int button) {
        return false;

    }

    public void mousePressed(StateBasedGame sb, int button) {
    }

    @Override
    public float getX() {
        return x;
    }

    @Override
    public float getY() {
        return y;
    }
}
